/**
 * @file env.c
 * @author liangwenhao (1943080020@qq.com)
 * @brief 
 * @version 0.1
 * @date 2021-07-21
 * 
 * @copyright Copyright (c) 2021
 * 
 * @authorGitee  https://gitee.com/liangwenhao
 * @authorGithub https://github.com/WHaoL
 * @authorBlogs https://blog.csdn.net/liangwenhao1108
 *  
 * @Code Description: 测试 getenv() setenv()/putenv()  获取和设置环境变量 P168-169
 *  
 */
#include "apue.h"
#include <stdlib.h>
#include <stdio.h>

int main(void)
{
    char *ptr = NULL;

    if ((ptr = getenv("USER")) != NULL) // "name"
        printf("USER=%s\n", ptr);

    if (setenv("USER", "test-setenv", 1) != 0) //"name" "value"
        err_sys("setenv err");
    if ((ptr = getenv("USER")) != NULL)
        printf("USER=%s\n", ptr);

    if (putenv("USER=test-putenv") != 0) // "name=value"
        err_sys("setenv err");
    if ((ptr = getenv("USER")) != NULL)
        printf("USER=%s\n", ptr);

    if (putenv("USER=lwh") != 0)
        err_sys("setenv err");
    if ((ptr = getenv("USER")) != NULL)
        printf("USER=%s\n", ptr);

    return 0;
}